﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using SMTC.BO.Role;
using SMTC.Model.Model;
using SMTC.WebCommon.Common;

public partial class RoleManagement_ModifyRoleInfo : BasePage
{
    RoleQueryService _service = new RoleQueryService();
    RoleProcessService _pro = new RoleProcessService();
    List<string> fun_id_list = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["action"] != null && Request["action"].ToString() == "upd")
        {
            ViewState["RoleID"] = Request["RoleID"].ToString();
            txtRoleName.Text = Request["RoleName"].ToString();
            txtRoleDescription.Text = Request["Description"].ToString();
            List<SMT_FunctionModel> functionlist = new List<SMT_FunctionModel>();
            List<SMT_RoleFunctionModel> rolefunctionlist = new List<SMT_RoleFunctionModel>();
            functionlist = _service.GetFunctionList();
            rolefunctionlist = _service.GetRoleFunctionList(ViewState["RoleID"].ToString());
            HtmlGenericControl span;
            HtmlGenericControl br;
            HtmlGenericControl empty;
            CheckBox check;
            for (int i = 1; i <= functionlist.Count; i++)
            {
                fun_id_list.Add(functionlist[i - 1].FunctionID);
                check = new CheckBox();
                check.ID = functionlist[i - 1].FunctionID;
                foreach (var role_in_function in rolefunctionlist)
                {
                    if (role_in_function.FunctionID == functionlist[i - 1].FunctionID)
                        check.Checked = true;
                }
                span = new HtmlGenericControl();
                span.TagName = "span";
                span.InnerText = functionlist[i - 1].FunctionName;
                empty = new HtmlGenericControl();
                empty.TagName = "span";
                empty.InnerHtml = "&nbsp;&nbsp;&nbsp;";
                authority.Controls.Add(check);
                authority.Controls.Add(span);
                authority.Controls.Add(empty);
                if (i % 4 == 0)
                {
                    br = new HtmlGenericControl();
                    br.TagName = "br";
                    authority.Controls.Add(br);
                }
            }
        }


    }

    protected void btnUpd_OnClick(object sender, EventArgs e)
    {
        List<SMT_RoleFunctionModel> new_rolefun_list = new List<SMT_RoleFunctionModel>();
        foreach (string fun_id in fun_id_list)
        {
            CheckBox ch = FindControl(fun_id) as CheckBox;
            if (ch.Checked)
            {
                SMT_RoleFunctionModel R_F=new SMT_RoleFunctionModel();
                R_F.FunctionID=fun_id;
                R_F.RoleID=ViewState["RoleID"].ToString();
                new_rolefun_list.Add(R_F);
            }
        }
       if(_pro.UPDATE_ROLE_FUCNTION(new_rolefun_list,ViewState["RoleID"].ToString()))
       {
           ShowMessage(Resources.Resource.Update_RoleInfo_Success);
       }
             else
        {
            ShowMessage(Resources.Resource.UpdateRoleFailed);//"维护贴片机失败！"
        }
    }
}